package cc.kzc.adminauth.config;

import java.time.Duration;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.ObjectUtils;

@Configuration
public class Redis0Config {

	/**
	 * pool配置
	 */
	@Value("${spring.redis0.timeout:30000}")
	private long timeout;

	@Value("${spring.redis0.lettuce.pool.max-active:8}")
	private int maxActive;

	@Value("${spring.redis0.lettuce.pool.max-wait:-1}")
	private long maxWait;

	@Value("${spring.redis0.lettuce.pool.max-idle:8}")
	private int maxIdle;

	@Value("${spring.redis0.lettuce.pool.min-idle:0}")
	private int minIdle;

	// redis 配置
	@Value("${spring.redis0.host}")
	private String redis_host;

	@Value("${spring.redis0.port}")
	private int redis_port;

	@Value("${spring.redis0.password}")
	private String redis_password;

	@Value("${spring.redis0.database:0}")
	private int redis_db;

	@Primary
	@Bean(name = "redisConnectionFactory")
	public RedisConnectionFactory redisConnectionFactory() {
		// 基本配置
		RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
		configuration.setHostName(redis_host);
		configuration.setPort(redis_port);
		configuration.setDatabase(redis_db);
		if (!ObjectUtils.isEmpty(redis_password)) {
			RedisPassword redisPassword = RedisPassword.of(redis_password);
			configuration.setPassword(redisPassword);
		}

		// 连接池通用配置
		GenericObjectPoolConfig<?> genericObjectPoolConfig = new GenericObjectPoolConfig<>();
		genericObjectPoolConfig.setMaxTotal(maxActive);
		genericObjectPoolConfig.setMaxWait(Duration.ofMillis(maxWait));
		genericObjectPoolConfig.setMaxIdle(maxIdle);
		genericObjectPoolConfig.setMinIdle(minIdle);

		// Lettuce Pool
		LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration
				.builder();
		builder.poolConfig(genericObjectPoolConfig);
		builder.commandTimeout(Duration.ofMillis(timeout));
		LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
		connectionFactory.afterPropertiesSet();
		return connectionFactory;
	}

	@Primary
	@Bean(name = "redisTemplate")
	public RedisTemplate<String, Object> redisTemplate(
			@Qualifier("redisConnectionFactory") RedisConnectionFactory factory) {

		RedisTemplate<String, Object> template = new RedisTemplate<>();
		// 配置连接工厂
		template.setConnectionFactory(factory);

//		// 使用FastJsonRedisSerializer来序列化和反序列化
//		FastJsonRedisSerializer<Object> fastJsonSerial = new FastJsonRedisSerializer<>(Object.class);

		// 使用StringRedisSerializer来序列化和反序列化redis的key值
		template.setKeySerializer(new StringRedisSerializer());
		// 值采用json序列化
		template.setValueSerializer(new StringRedisSerializer());

		// 设置hash key 和value序列化模式
		template.setHashKeySerializer(new StringRedisSerializer());
		template.setHashValueSerializer(new StringRedisSerializer());

		template.afterPropertiesSet();

		return template;
	}

}
